﻿namespace Leetcode.N1115;

using System.Threading;
public class FooBar
{
    private int n;

    private AutoResetEvent fooResetEvent = new AutoResetEvent(true);
    private AutoResetEvent barResetEvent = new AutoResetEvent(false);

    public FooBar(int n)
    {
        this.n = n;
    }

    public void Foo(Action printFoo)
    {

        for (int i = 0; i < n; i++)
        {
            this.fooResetEvent.WaitOne();
            printFoo();
            this.barResetEvent.Set();
        }
    }

    public void Bar(Action printBar)
    {

        for (int i = 0; i < n; i++)
        {
            this.barResetEvent.WaitOne();
            printBar();
            this.fooResetEvent.Set();
        }
    }
}